<?php

namespace app\model;

use think\Model;
use think\Cache;
use think\Db;
class Role extends Model{

    protected $table = 'mm_role';
    /**
     * Undocumented 角色
     *
     * @param [type] $id
     * @return void
     */
    public static function roleType($id){
        $role = Role::get($id);
        return $role->role_name;
    }
    /**
     * Undocumented 获取下拉角色列表
     *
     * @param [type] $pid
     * @return void
     */
    public static function selectRole($cid = 0,$rid = 0 ){
        $list = Role::where('c_id',$cid)->select();
        //print_r($list);die;
        $html = "<select id='role_id' class='form-control m-b' name='role_id'>";
        foreach($list as $k => $v){
            $selected = "";
            if($v['id'] == $rid){
                $selected = 'selected';
            }
            
            $html .= '<option '.$selected.' value='.$v['id'].'>'.$v['role_name'].'</option>';
        }
        $html .= "</select>";
        return $html;
    }

    /**
     * Undocumented 获取等级
     * @return void
     */
    public static function getLevel($lid = 0){
        $level = array(
            array("id"=> 1,"level"=>"超级管理员"),
        array("id"=> 2,"level"=>"董事"),
        array("id"=> 3,"level"=>"总公司财务"),
        array("id"=> 4,"level"=>"业务员"),
        array("id"=> 5,"level"=>"分公司财务") );
        $html = "<select id='level' class='form-control m-b' name='level'>";
        foreach($level as $k => $v){
            $selected = "";
            if($v['id'] == $lid){
                $selected = 'selected';
            }
            
            $html .= '<option '.$selected.' value='.$v['id'].'>'.$v['level'].'</option>';
        }
        $html .= "</select>";
        return $html;
    }

    /**
     * 添加/编辑职务
     * @param [type] $data
     * @return void
     */
    public static function editPosition($data){
        $Aes = new \Aes();
        Db::startTrans();
        try {
            if(!empty($data['id'])){//更新
                Role::where("id",$data['id'])->update(array(
                    "role_name" =>trim($data['role_name']),
                    "c_id" =>$data['cid'],
                    "level" =>$data['level'],
                 ));
              
            }else{//新增账号
                Role::insertGetId(array(
                    "role_name" =>trim($data['role_name']),
                    "c_id" =>$data['cid'],
                    "level" =>$data['level'],
                    "create_time"=> date('Y-m-d H:i:s',time())
                ));
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $e->getMessage();
        }
        return true;
    }
    

}




    





?>